<template>
  <div class="emergency_box">
    <div class="emergency_top">
      <div class="emergency_title">故障类型分布</div>
      <div class="emergency_month">
        <div :class="searchDateNumber == 2? 'emergency_ThisMarch1':'emergency_March'" @click="getList(2)"><span>近三个月</span></div>
        <div :class="searchDateNumber == 1? 'emergency_ThisMarch1':'emergency_LastMarch'" @click="getList(1)"><span>上月</span></div>
        <div :class="searchDateNumber == 0? 'emergency_ThisMarch2':'emergency_ThisMarch'" @click="getList(0)"><span>本月</span></div>
      </div>
    </div>
    <div v-if="tableDate&&tableDate.length>0" class="echarts_container">
      <div id="distribution" class="col-md-12  col-sm-12 col-xs-12" style="height: 100%;" />
    </div>
    <div v-else class="echarts_noData">
      <svg-icon icon-class="暂无数据" />
    </div>
  </div>
</template>

<script>
import { deviceRepairTypeDistribution } from '@/api/equipmentManage/equipmentReport/dataBoard'
import { mapGetters } from 'vuex'
export default {
  name: 'EmergencyDegreeDistribution',
  computed: {
    ...mapGetters([
      'windowResizeTag'
    ])
  },
  watch: {
    windowResizeTag() {
      if (this.myChart) {
        this.$nextTick(() => {
          setTimeout(() => {
            this.drawLine()
          }, 500)
        })
      }
    }
  },
  data() {
    return {
      msg: 'fafa',
      tableDate: [1],
      searchDateNumber: 0,
      pageData: {
        searchDateNumber: 0,
        isContinuous: 0
      },
      timer: undefined,
      myChart: undefined
    }
  },
  mounted() {
    this.drawLine()
    this.getList(0)
  },
  beforeDestroy() {
    // 页面关闭清除定时器
    clearInterval(this.timer)
  },
  methods: {
    getList(num) {
      clearInterval(this.timer)
      this.searchDateNumber = num
      switch (num) {
        case 0:
          this.pageData.searchDateNumber = 0
          this.pageData.isContinuous = 0
          break
        case 1:
          this.pageData.searchDateNumber = 1
          this.pageData.isContinuous = 0
          break
        case 2:
          this.pageData.searchDateNumber = 2
          this.pageData.isContinuous = 1
          break
      }
      this.getListData(this.pageData.searchDateNumber, this.pageData.isContinuous)
      this.timer = setInterval(() => {
        this.getListData(this.pageData.searchDateNumber, this.pageData.isContinuous)
      }, this.$store.state.common.refreshFrequency * 1000)
    },
    getListData(searchDateNumber, isContinuous) {
      deviceRepairTypeDistribution({
        searchDateNumber: searchDateNumber,
        isContinuous: isContinuous
      }).then(res => {
        this.tableDate = res.data
        this.$nextTick(() => {
          this.drawLine()
        })
      })
    },

    // getLastMarchList() {
    //   this.searchDateNumber = 1
    //   deviceRepairTypeDistribution({
    //     searchDateNumber: 1,
    //     isContinuous: 0
    //   }).then(res => {
    //     this.tableDate = res.data
    //     this.$nextTick(() => {
    //       this.drawLine()
    //     })
    //   })
    // },
    // getMarchList() {
    //   this.searchDateNumber = 2
    //   deviceRepairTypeDistribution({
    //     searchDateNumber: 2,
    //     isContinuous: 1
    //   }).then(res => {
    //     this.tableDate = res.data
    //     this.$nextTick(() => {
    //       this.drawLine()
    //     })
    //   })
    // },
    drawLine() {
      if (this.myChart) {
        this.$echarts.init(document.getElementById('distribution')).dispose()
      }
      this.myChart = this.$echarts.init(document.getElementById('distribution'))
      if (!this.tableDate || this.tableDate.length == 0) {
        return
      }
      const arr = this.tableDate
      this.myChart.setOption({
        grid: {
          show: false,
          top: '20',
          right: '20',
          bottom: '40',
          left: '60'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        color: ['#5AD1FF'],
        xAxis: {
          type: 'category',
          data: arr.map(item => item.name),
          axisTick: {
            show: false
          },
          offset: 2,
          axisLabel: {
            show: true,
            color: '#999999', // 更改坐标轴文字颜色
            textStyle: {
              fontSize: 12 // 更改坐标轴文字大小
            }
          },
          axisLine: {
            show: false
          }
        },
        yAxis: {
          type: 'value',
          // 影藏y轴刻度
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            show: true,
            color: '#999999', // 更改坐标轴文字颜色
            textStyle: {
              fontSize: 12 // 更改坐标轴文字大小
            },
            margin: 20
          }
        },
        series: [
          {
            data: arr.map(item => item.repairTypeNumber),
            silent: true,
            type: 'bar',
            itemStyle: {
              normal: {
                // color: '#FF9ADC',
                barBorderRadius: [20, 20, 0, 0]
                // shadowColor: 'rgba(0, 0, 0, 0.3)',
                // shadowOffsetX: 2, // 折线的X偏移
                // shadowOffsetY: 0, // 折线的Y偏移
                // shadowBlur: 6 // 折线模糊
              }
            },
            barWidth: 12 // 柱图宽度ƒ
          }
        ]
      })
    }
  }
}
</script>

<style lang="less" scoped>
.emergency_box{
  width: 100%;
  height: calc(100% - 50px);
}
.emergency_top{
  display: flex;
  justify-content: space-between;
  .emergency_title{

    font-size: 20px;

    font-weight: bold;
    line-height: 34px;
    letter-spacing: -0.21px;
    color: #333333;
    padding-top: 16px;
    padding-left: 20px;
  }
  .emergency_month{
    display: flex;
    font-size: 18px;
    font-weight: normal;
    line-height: 25px;
    letter-spacing: -0.16px;
    color: #999999;
    padding-top: 22px;
    padding-right: 20px;
    cursor: pointer;
    .emergency_ThisMarch1{

      font-size: 18px;
      font-weight: normal;
      line-height: 25px;
      letter-spacing: -0.16px;
      color: #387BFC;
      padding-right: 30px;
    }
    .emergency_ThisMarch2{

      font-size: 18px;
      font-weight: normal;
      line-height: 25px;
      letter-spacing: -0.16px;
      color: #387BFC;
    }
    .emergency_March{
      padding-right: 30px;
    }
    .emergency_LastMarch{
      padding-right: 30px;
    }
  }
}
.echarts_container{
  width: 100%;
  height: 100%;
}
</style>

